package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.modules.generator.dao.DtAreaDao;
import io.renren.modules.generator.entity.DtAreaEntity;
import io.renren.modules.generator.service.DtAreaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("dtAreaService")
public class DtAreaServiceImpl extends ServiceImpl<DtAreaDao, DtAreaEntity> implements DtAreaService {

    @Autowired
    private DtAreaDao dtAreaDao;

    @Override
    public List<DtAreaEntity> queryPage(Map<String, Object> params) {

        return dtAreaDao.queryPage(params);

      /*  String id = (String) params.get("id");
        String areaName = (String) params.get("areaName");
        String mnemonicCode = (String) params.get("mnemonicCode");
        String omsMnemonicCode = (String) params.get("omsMnemonicCode");


        Page<DtAreaEntity> page = this.selectPage(
                new Query<DtAreaEntity>(params).getPage(),
                new EntityWrapper<DtAreaEntity>()
                .eq(StringUtils.isNotBlank(id),"id",id)
                        .like(StringUtils.isNotBlank(areaName),"area_name",areaName)
                        .like(StringUtils.isNotBlank(mnemonicCode),"mnemonic_code",mnemonicCode)
                        .like(StringUtils.isNotBlank(omsMnemonicCode),"oms_mnemonic_code",omsMnemonicCode)
                        .orderBy(true,"oms_mnemonic_code",false)
        );*/
    }

    @Override
    public int queryPageTotal(Map<String, Object> params) {

        return dtAreaDao.queryPageTotal(params);
    }

    @Override
    public List<Map<String, String>> queryOmsMnemonicCode(String code) {
        return dtAreaDao.queryOmsMnemonicCode(code);
    }

    @Override
    public DtAreaEntity queryEntityByOmsMnemonicCode(String code) {
        return this.selectOne(new EntityWrapper<DtAreaEntity>()
                .eq(StringUtils.isNotBlank(code),"oms_mnemonic_code",code));
    }

    @Override
    public DtAreaEntity queryEntutyByAreaNameAndLevel(String areaName, int level) {
        return this.selectOne(new EntityWrapper<DtAreaEntity>()
                .eq(StringUtils.isNotBlank(areaName),"area_name",areaName)
                .eq(true,"area_level",level));
    }



    @Override
    public R save(DtAreaEntity dtArea) {
        DtAreaEntity dtAreaEntity = this.selectOne(new EntityWrapper<DtAreaEntity>()
                .eq(StringUtils.isNotBlank(dtArea.getOmsMnemonicCode()), "oms_mnemonic_code", dtArea.getOmsMnemonicCode()));

        if(dtAreaEntity == null){
            this.insert(dtArea);
            return  R.ok();
        }
        return R.error(dtArea.getOmsMnemonicCode()+"已经存在");
    }

    @Override
    public R update(DtAreaEntity dtArea) {
        DtAreaEntity dtAreaEntity = this.selectOne(new EntityWrapper<DtAreaEntity>()
                .eq(StringUtils.isNotBlank(dtArea.getOmsMnemonicCode()), "oms_mnemonic_code", dtArea.getOmsMnemonicCode()));
        if(dtAreaEntity != null && dtAreaEntity.getId().intValue() != dtArea.getId().intValue()){
            return R.error(dtArea.getOmsMnemonicCode()+"已经存在");
        }

        this.updateById(dtArea);
        return R.ok();

    }

}
